<?php
/**
 * Created by 2019/4/13 0013
 * Effect: TokenValidationMiddleware.php
 * Author: 品花
 * Date: 2019/4/13 0013
 * Time: 上午 10:37
 */

namespace App\Middleware;

use App\Consts\RedisKey;
use App\HttpController\Api\V1\Test;
use App\Model\Admin;
use App\Model\User;
use App\Utility\Register;
use App\Utility\Response;
use EasySwoole\EasySwoole\Config;
use EasySwoole\EasySwoole\ServerManager;
use EasySwoole\Jwt\Jwt;
use EasySwoole\RedisPool\Redis;

class TokenValidationMiddleware
{
    protected static $instance;

    public static function getInstance()
    {
        if ( !isset(self::$instance)) {
            self::$instance = new static();
        }
        return self::$instance;
    }

    public function verification($request)
    {
            $path    = $request->getUri()->getPath();
            if($path == "/"){
                return  true;
            }
            $module = explode('/',$path)[1];
            $routes  = Config::getInstance()->getConf('pass_route');

            //免认证和免登录接口直接跳过token验证
            if(!in_array($path,$routes)){
                $token = $request->getRequestParam('token');

                if(!$token){
                    Response::responseJson(1003);
                    return false;
                }

                //前台
                if($module == 'v1'){

                    try{

                        $jwtObject = Jwt::getInstance()->decode($token);
                        $status = $jwtObject->getStatus();
                        if($status != 1){

                            $codes = [-1=> 1001,-2 => 900 ];
                            Response::responseJson($codes[$status]);
                            return false;
                        }
                        if(!isset($jwtObject->getData()['user_info'])){
                            Response::responseJson(1001);
                            return false;
                        }
                        //验证是不是最新token
                        $serverToken = Redis::defer('redis')->get(RedisKey::loginUserToken($jwtObject->getAud()));
                        if(!$serverToken){
                            Response::responseJson(1001);
                            return false;
                        }
                        if($serverToken != $token){
                            Response::responseJson(1001);
                            return false;
                        }
                        $fd = Redis::defer('redis')->hGet(RedisKey::WS_USER_FD_HASH_LIST,'uid:'.$jwtObject->getAud());
                        if(!$fd){
                            if($serverToken != $token){
                                var_dump('掉线');
                                Response::responseJson(1047);
                                return false;
                            }
                        }
                        Register::getInstance()->set('uid',$jwtObject->getAud());
                        Register::getInstance()->set('user_info',$jwtObject->getData()['user_info']);

                        return true;
                    }catch (\Exception $e){
//                        var_dump();
                        Response::responseJson(1001,$e->getMessage());
                        return false;
                    }


                //后台
                }elseif($module == "admin") {

                    try{

                        $jwtObject = Jwt::getInstance()->decode($token);
                        $status = $jwtObject->getStatus();
                        if($status != 1){

                            $codes = [-1=> 1001,-2 => 900 ];
                            Response::responseJson($codes[$status]);
                            return false;
                        }
                        if(!isset($jwtObject->getData()['manager_info'])){
                            Response::responseJson(1001);
                            return false;
                        }

                        Register::getInstance()->set('mid',$jwtObject->getAud());
                        Register::getInstance()->set('manager_info',$jwtObject->getData()['manager_info']);

                        return true;
                    }catch (\Exception $e){
//                        var_dump();
                        Response::responseJson(1001,$e->getMessage());
                        return false;
                    }
                }



            }



            return true;

    }


}